import Qt 4.7
import QtQuick 1.0

Rectangle {
  id: loading
  color: "#000000"

  property alias editor: editor 
  property bool is_response: false
  property bool submitting: false
  property int plurk_id: 0

  signal plurkSubmitted()
  signal plurkSubmitting()

  Rectangle {
    id: content
    x: 5
    y: 5
    width: parent.width - 10
    height: loading.height - button1.height - 20
    color: "#000000"
    border.width: 2
    border.color: "#aaa"
    smooth: true

    TextEdit {
       x: 5
       y: 5
       width: parent.width - 10
       height: parent.height - 10
       id: editor
       wrapMode: TextEdit.Wrap
       textFormat: TextEdit.PlainText
       color: "#ffffff"
       focus: true
       onTextChanged : {
         length.text = editor.text.length
       }
    }
  }

  Button {
    id: button1
    x: 5
    y: content.y + content.height + 5
    width: 140
    height: 32
    text: "Plurk"
    onClicked : {
      if (submitting) return;
      if (editor.text.length < 140) {
        if (is_response) {
          plurk.ResponseAdd(plurk_id, editor.text);
        } else {
          plurk.PlurkAdd(editor.text);
        }
        submitting = true;
        loading.plurkSubmitting();

        submitting = false;
        loading.plurkSubmitted();
      }
    }
  }

  Text {
    id: length
    color: editor.text.length < 140 ? "#ffffff" : "#ff0000"
    x: button1.width + button1.x
    y: button1.y
    text: "0"
    width: loading.width - length.x - 5
    horizontalAlignment: Text.AlignRight
  }

  Connections {
    target: plurk
    onPlurkAdded : {
      submitting = false;
      loading.plurkSubmitted();
    }
    onResponseAdded: {
      submitting = false;
      loading.plurkSubmitted();
    }
    onPlurkError : {
      submitting = false;
    }
  }

}
